//
//  MainViewController.m
//  STBaseViewController
//
//  Created by lvjia on 13-12-27.
//  Copyright (c) 2013年 stlwtr. All rights reserved.
//

#import "MainViewController.h"
#import "STAppDelegate.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSDictionary *params = @{@"name": @"yls", @"pwd": @"123"};
    
//    [ApplicationDelegate.mainEngine sendAsynchronousRequestWithPath:@"login.php" params:params method:STRequestMethodPost completionHandler:^(NSString *responseString, id responseData, NSError *connectionError) {
//        id results = responseData;
//        NSLog(@"%@", results);
//    }];
    
    // 上传图片
//    UIImage *image = [UIImage imageNamed:@"sample.png"];
//    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
//    
//    STRequestEngine *upEngine = [[STRequestEngine alloc] initWithHostName:@"testbed2.mknetworkkit.com"];
//    [upEngine uploadImageWithUrl:@"upload.php" params:@{@"Submit": @"YES"} imageData:imageData imageName:@"aaa" progressChanged:^(double progress) {
//        DLog(@"upload progress = %f", progress);
//    } completionHandler:^(NSString *responseString, id responseData, NSError *connectionError) {
//        DLog(@"upload completion");
//    }];
    
    STRequestEngine *dnEngine = [[STRequestEngine alloc] initWithHostName:@"testbed2.mknetworkkit.com"];
    
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *downPath = [docPath stringByAppendingString:@"down.pdf"];
//    [dnEngine registerOperationSubclass:[STRequest class]];
    STRequest *request = [dnEngine downloadFileFrom:@"http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/NSURLRequest_Class.pdf" toFile:downPath progressChanged:^(double progress) {
        DLog(@"download progress = %f", progress);
        
    } completionHandler:^(NSString *responseString, id responseData, NSError *connectionError) {
        DLog(@"download completion");
    }];
    
    [request pause];
    
    sleep(20);
    [request resume];
    
//    sleep(10);
//    [dnOp cancel];
//    
//    sleep(10);
//    [dnOp start];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - keyboard show & hide

- (void)keyboardWillShowWithKeyboardFrame:(CGRect)keyboardFrame andDuration:(NSTimeInterval)duration
{
    __block UITextField *tf = _textField;
    self.methodWhenKeyboardShow = ^{
        CGRect frame = tf.frame;
        frame.origin.y = keyboardFrame.origin.y - tf.frame.size.height;
        tf.frame = frame;
    };
}

- (void)keyboardWillHideWithKeyboardFrame:(CGRect)keyboardFrame andDuration:(NSTimeInterval)duration
{
    __block UITextField *tf = _textField;
    
    self.methodWhenKeyboardHide = ^{
        CGRect frame = tf.frame;
        frame.origin.y = keyboardFrame.origin.y - tf.frame.size.height;
        tf.frame = frame;
    };
}

- (IBAction)tapBackground:(id)sender {
    [_textField resignFirstResponder];
}

@end
